﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;
using TimeTableEasy.src.Core;

namespace TimeTableEasy
{
    public partial class Campus_Choose : Form
    {
        private OpCodes.TimeTableEasyAction _Action;
        private ArrayList _IdList = new ArrayList();
        private UInt16 _IdCampus = 0;

        public Campus_Choose(OpCodes.TimeTableEasyAction action)
        {
            InitializeComponent();

            this._Action = action;

            OracleDataReader array = OracleConnector.Query("SELECT CAM_NUM, CAM_NOM FROM CAMPUS ORDER BY (CAM_NOM)");

            if (array == null) return;

            while (array.Read())
            {
                _IdList.Add((UInt16)array.GetDecimal(0));
                List_Campus.Items.Add(array.GetString(1));
            }

            array.Dispose();
        }

        private void validerBtn_Click(object sender, EventArgs e)
        {
            if ((_Action == OpCodes.TimeTableEasyAction.ACTION_MOD))
            {
                new Campus_Edit(this._Action, _IdCampus).ShowDialog();
                this.Close();
            }
            else if (_Action == OpCodes.TimeTableEasyAction.ACTION_DEL)
            {
                OracleDataReader array = OracleConnector.Query("DELETE FROM ETUDIANT_CAMPUS_PERIODE WHERE ECP_CAMPUSNUM = " + _IdCampus);
                OracleDataReader array1 = OracleConnector.Query("DELETE FROM CAMPUS_CURSUS WHERE CC_CAMPUSNUM = " + _IdCampus);
                OracleDataReader array2 = OracleConnector.Query("DELETE FROM DISPENSER WHERE Dis_CAMPUSNUM = " + _IdCampus);
                OracleDataReader array3 = OracleConnector.Query("DELETE FROM CAMPUS WHERE CAM_NUM = " + _IdCampus);

                if ((array == null) || (array1 == null) || (array2 == null) || (array3 == null))
                    Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_DEL_ERROR);
                else
                    Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_DEL);
            }
        }

        private void campusList_SelectedIndexChanged(object sender, EventArgs e)
        {
            _IdCampus = (UInt16)_IdList[UInt16.Parse(List_Campus.SelectedIndex.ToString())];
        }
    }
}
